/**
 * 
 */
package com.monte.export.excel.template;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.commons.lang.StringUtils;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**  
 * <b>System：</b>NDMP<br/>
 * <b>Title：</b>XmlTemplateParser.java<br/>
 * <b>Description：</b> xml模板解析工具<br/>
 * <b>@author： </b>fengmengyue<br/>
 * <b>@date：</b>2014-4-2 上午11:14:44<br/>  
 * <b>@version：</b> 1.0.0.0<br/>
 * <b>Copyright (c) 2014 ASPire Tech.</b>   
 *   
 */
public class XmlTemplateParser {
	/**
	 * 模板文件
	 */
	private File file;
	public XmlTemplateParser(File file){
		if(file == null || !file.isFile()){
			throw new IllegalArgumentException("非法模板文件:" + file.getPath());
		}
		this.file = file;
	}
	
	private void parseTd(Td td,Attributes attributes){
		td.setLabel(attributes.getValue("label"));
		if(StringUtils.isNotEmpty(attributes.getValue("align"))){
			td.setAlign(attributes.getValue("align"));
		}
		if(StringUtils.isNotEmpty(attributes.getValue("colspan")) && StringUtils.isNumeric(attributes.getValue("colspan"))){
			td.setColspan(Integer.parseInt(attributes.getValue("colspan")));
		}
		if(StringUtils.isNotEmpty(attributes.getValue("rowspan")) && StringUtils.isNumeric(attributes.getValue("rowspan"))){
			td.setRowspan(Integer.parseInt(attributes.getValue("rowspan")));
		}
		if(StringUtils.isNotEmpty(attributes.getValue("width")) && StringUtils.isNumeric(attributes.getValue("width"))){
			td.setWidth(Integer.parseInt(attributes.getValue("width")));
		}
		if(StringUtils.isNotEmpty(attributes.getValue("height")) && StringUtils.isNumeric(attributes.getValue("height"))){
			td.setHeight(Integer.parseInt(attributes.getValue("height")));
		}
		if(StringUtils.isNotEmpty(attributes.getValue("datatype"))){
			td.setDatatype(attributes.getValue("datatype"));
		}
		if(StringUtils.isNotEmpty(attributes.getValue("dateformat"))){
			td.setDateFormat(attributes.getValue("dateformat"));
		}
		//   
		if(StringUtils.isNotEmpty(attributes.getValue("textColor"))){
			td.setTextColor(attributes.getValue("textColor"));
		}
		if(StringUtils.isNotEmpty(attributes.getValue("fontSize"))){
			td.setFontSize(attributes.getValue("fontSize"));
		}
		if(StringUtils.isNotEmpty(attributes.getValue("backgroundColor"))){
			td.setBackgroundColor(attributes.getValue("backgroundColor"));
		}
		if(StringUtils.isNotEmpty(attributes.getValue("border"))){
			td.setBorder(attributes.getValue("border"));
		}
		if(StringUtils.isNotEmpty(attributes.getValue("borderColor"))){
			td.setBorderColor(attributes.getValue("borderColor"));
		}
	}
	
	private void parseSheet(SheetStruct sheet,Attributes attributes){
		sheet.setSheetId(attributes.getValue("id"));
		sheet.setSheetName(attributes.getValue("name"));
	}
	
	private void parseTable(Table table,Attributes attributes){
		if(StringUtils.isNotEmpty(attributes.getValue("textColor"))){
			table.setTextColor(attributes.getValue("textColor"));
		}
		if(StringUtils.isNotEmpty(attributes.getValue("fontSize"))){
			table.setFontSize(attributes.getValue("fontSize"));
		}
		if(StringUtils.isNotEmpty(attributes.getValue("backgroundColor"))){
			table.setBackgroundColor(attributes.getValue("backgroundColor"));
		}
		if(StringUtils.isNotEmpty(attributes.getValue("border"))){
			table.setBorder(attributes.getValue("border"));
		}
		if(StringUtils.isNotEmpty(attributes.getValue("borderColor"))){
			table.setBorderColor(attributes.getValue("borderColor"));
		}
	}
	
	public List<SheetStruct> parse(){
		final List<SheetStruct> sheets = new LinkedList<SheetStruct>();
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();  
			SAXParser parser = factory.newSAXParser();  
			parser.parse(file, new DefaultHandler(){
				private SheetStruct sheet;
				private Table table;
				private Tr tr;
				private Td td;
				public void startElement(String uri, String localName, String qName, org.xml.sax.Attributes attributes) throws SAXException {
					if("sheet".equals(qName)){
						sheet = new SheetStruct();
						parseSheet(sheet, attributes);
					}else if("title".equals(qName)){
						table = new Table();
						parseTable(table, attributes);
					}else if("body".equals(qName)){
						table = new Table();
					}else if("foot".equals(qName)){
						table = new Table();
					}else if("tr".equals(qName)){
						tr = new Tr();
						tr.setTable(table);
					}else if("td".equals(qName)){
						td = new Td();
						td.setTr(tr);
						parseTd(td, attributes);
					}
				};
				public void endElement(String uri, String localName, String qName) throws SAXException {
					if("sheet".equals(qName)){
						sheets.add(sheet);
						sheet = null;
					}else if("title".equals(qName)){
						sheet.setTitle(table);
						table = null;
					}else if("body".equals(qName)){
						sheet.setBody(table);
						table = null;
					}else if("foot".equals(qName)){
						sheet.setFoot(table);
						table = null;
					}else if("tr".equals(qName)){
						if(table.getTrs() == null){
							table.setTrs(new LinkedList<Tr>());
						}
						table.getTrs().add(tr);
						tr = null;
					}else if("td".equals(qName)){
						if(tr.getTds() == null){
							tr.setTds(new LinkedList<Td>());
						}
						tr.getTds().add(td);
						td = null;
					}
				};
			});
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		} catch (SAXException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} catch (ParserConfigurationException e) {
			throw new RuntimeException(e);
		}
		return sheets;
	}

}
